package com.ksv.qlsv;

import java.util.ArrayList;
import com.ksv.lop.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TableRow;

public class LopHocDeleteActivity extends Activity {

	private ListView lv_mon_hoc;
	private CheckBox ch_chon_het;
	private NewAdapter newAdapter;
	public static Button btn_xoa;
	private Button btn_huy;
	private TableRow tblrow;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frmmonhoc_del);   
        this.setTitle(getResources().getString(R.string.lop));
        lv_mon_hoc = (ListView)findViewById(R.id.lv_mon_hoc_del);
        ch_chon_het = (CheckBox)findViewById(R.id.ch_chon_het);
        ch_chon_het.setChecked(false);
		ch_chon_het.setEnabled(true);
        tblrow = (TableRow) findViewById(R.id.tableRow1);
        btn_xoa = (Button)tblrow.findViewById(R.id.btn_xoa);
        btn_xoa.setEnabled(false);
        btn_xoa.setText(getResources().getString(R.string.xoa));
        btn_huy = (Button)tblrow.findViewById(R.id.btn_huy);
        btn_huy.setText(getResources().getString(R.string.huy));
        onCheckedChangedSelectAll();
        onClickButtonDelete();
        onClickButtonCancel();
        loadListLopHoc();
    }
	
	private void onClickButtonCancel() {
		// TODO Auto-generated method stub
		try{
			btn_huy.setOnClickListener(new OnClickListener() {				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					LopHocDeleteActivity.this.finish();
				}
			});
		}
		catch(Exception Ex)
		{
			
		}
	}

	private void onClickButtonDelete() {
		// TODO Auto-generated method stub
		try{
			btn_xoa.setOnClickListener(new OnClickListener() {				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog diag = Alerts.ShowDeleteDialog(LopHocDeleteActivity.this);
					diag.setOnDismissListener(new OnDismissListener() {				
						@Override
						public void onDismiss(DialogInterface dialog) {
							// TODO Auto-generated method stub
							if(Alerts.bXoa == true){
								if(ch_chon_het.isChecked() == true){
									ConnectDB.deleteLopHoc();
									newAdapter.Clear();
									ch_chon_het.setChecked(false);
									ch_chon_het.setEnabled(false);
								}
								else{
									ArrayList<GetData> mang = newAdapter.mArrGetData;
									for (int i = mang.size() - 1; i >= 0; i--) {
										if(mang.get(i).getChecked() == true){
											LopHoc lop = new LopHoc();
											lop.setMaLop(mang.get(i).getID());
											ConnectDB.deleteLopHoc(lop);
											newAdapter.Remove(i);
										}
									}
									btn_xoa.setEnabled(false);
								}
							}
							dialog.dismiss();
						}
					});
					diag.show();					
				}
			});
		}
		catch(Exception Ex)
		{
			
		}
	}

	private void onCheckedChangedSelectAll(){
		try{
			ch_chon_het.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
					// TODO Auto-generated method stub
					if(arg0.isChecked() == true){
						setCheckedAll();
					}
					else{
						setUnCheckeAll();
					}
				}
				private void setUnCheckeAll() {
					newAdapter.setUnCheckedAll();
					btn_xoa.setEnabled(false);
				}
				private void setCheckedAll(){
					newAdapter.setCheckedAll();
					btn_xoa.setEnabled(true);
				}
			});
		}
		catch(Exception Ex)
		{
			
		}
	}
	
	private void loadListLopHoc(){
		try{
			lv_mon_hoc.setAdapter(null);
    	    Cursor c = ConnectDB.getAllLopHoc();
    	    NewAdapter.mID = ConnectDB.malop;
    	    NewAdapter.mName = ConnectDB.tenlop;
    	    NewAdapter.mIndex = 2;
    	    newAdapter = new NewAdapter(this, c);
    	    lv_mon_hoc.setAdapter(newAdapter);
    	}
    	catch(Exception ex)
		{
    		
		}
	}
}
